package com.dictionary.hi;

import com.inapp.util.BillingService;
import com.mango9x.utils.HttpHelpers;
import com.mango9x.utils.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.Theme;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ProgressBar;

public class SplashActivity extends Activity {
	protected boolean _active = true;
	protected int _splashTime = 3000;
	private static final int STEP_COUNT = 500;
	private ProgressBar progressBar;
	private boolean isDeviceOnline;
	private String newestCatResp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.splash_screen);
			progressBar = (ProgressBar) findViewById(R.id.progressbar);
			progressBar.setMax(STEP_COUNT);
			showProgress();
			downloadCategoryinfo();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void showProgress() {
		new AsyncTask<String, Integer, String>() {

			protected void onProgressUpdate(Integer... args) {
				try {
					progressBar.setProgress(args[0]);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			protected String doInBackground(String... args) {
				try {
					final long startTime = System.currentTimeMillis();
					final int interval = _splashTime / STEP_COUNT;
					long sleepTime;
					for (int i = 1; i <= STEP_COUNT; i++) {
						publishProgress(i);
						sleepTime = startTime + (i * interval)
								- System.currentTimeMillis();
						if (sleepTime > 0) {
							Thread.sleep(sleepTime);
						}
						if (isFinishing()) {
							break;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		}.execute();
	}

	private void downloadCategoryinfo() {
		new AsyncTask<String, String, String>() {
			protected String doInBackground(String... arg0) {
				try {
					isDeviceOnline = Utils.isDeviceOnline(SplashActivity.this);
					if (isDeviceOnline) {
						SharedPreferences sharedPreferences = PreferenceManager
								.getDefaultSharedPreferences(SplashActivity.this);
						boolean isProVersion = BillingService.isPurchased(
								sharedPreferences, Extra.IN_APP_PRODUCT_ID)
								|| getPackageName().endsWith("pro");
						String url = isProVersion ? getString(R.string.category_images_url_pro)
								: getString(R.string.category_images_url);

						String catUrl = url + 11;
						newestCatResp = HttpHelpers.getStringfromURL(catUrl);
					} else {
						Thread.sleep(_splashTime);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			protected void onPostExecute(String unused) {
				try {
					showHomepage();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.execute();
	}

	private synchronized void showHomepage() {
		try {
			if (_active && (!isFinishing())) {
				_active = false;

				if (Utils.isDeviceOnline(this)) {
					Intent intent = new Intent(SplashActivity.this,
							MainTabHostActivity.class);
					intent.putExtra(Extra.IS_ONLINE, isDeviceOnline);
					if (isDeviceOnline) {
						intent.putExtra(Extra.NEWEST_RESP, newestCatResp);
					}
					startActivity(intent);
					finish();
				} else {
					showOfflineAlert();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showOfflineAlert() {
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.message_go_online))
				.setCancelable(false)
				.setPositiveButton(getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(final DialogInterface dialog,
									final int id) {
								finish();
							}
						});
		final AlertDialog alert = builder.create();
		alert.setTitle(getString(R.string.info));
		alert.show();
	}

}